﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module;
using System.Data.SqlClient;
using Qmaker.DAL;
using System.Data;

namespace DLL
{
    public class DTag
    {
        #region SQL语句
        private const string SQL_INSERT = "insert into Tag(TagId,Name,DateCreated) values (@TagId,@Name,@DateCreated)";

       

        private const string SQL_SELECT = "select TagId,Name,DateCreated from Tag";


        #endregion

        #region 公共接口

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="tagInfo"></param>
        /// <returns></returns>
        public bool Insert(TagInfo tagInfo)
        {
            if (tagInfo == null)
                throw new ArgumentNullException("tagInfo");

            SqlParameter[] parms = GetInsertParms(tagInfo);
            //parms = SetInsertParmsCheck(parms);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        Convert.ToInt32(SqlHelper.ExecuteScalar(trans, CommandType.Text, SQL_INSERT, parms));
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 获取所有标签
        /// </summary>
        /// <returns></returns>
        public List<TagInfo> GetAllTagInfo()
        {
            List<TagInfo> tags = new List<TagInfo>();

            TagInfo tagInfo = null;
            
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT, null))
            {
                while (reader.Read())
                {
                    tagInfo = GetInstanceByDataReader(reader);
                    if (tagInfo != null && tagInfo.TagId != 0)
                        tags.Add(tagInfo);
                }
            }
            return tags;
        }

        
        

        #endregion

        #region 私有接口
        private SqlParameter[] SetInsertParmsCheck(SqlParameter[] parms)
        {
            for (int i = 0; i < parms.Length; i++)
            {
                if (parms[i].Value == null)
                {
                    parms[i].Value = DBNull.Value;
                }
                if (parms[i].DbType == DbType.DateTime && parms[i].Value.Equals(DateTime.MinValue))
                {
                    parms[i].Value = DBNull.Value;
                }
            }
            return parms;
        }

        private SqlParameter[] GetInsertParms(TagInfo tagInfo)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@TagId", SqlDbType.BigInt,8),
					new SqlParameter("@Name", SqlDbType.NVarChar,20),
					new SqlParameter("@DateCreated", SqlDbType.DateTime)};
            parameters[0].Value = tagInfo.TagId;
            parameters[1].Value = tagInfo.Name;
            parameters[2].Value = tagInfo.DateCreated;
            return SetInsertParmsCheck(parameters);
        }
        private TagInfo GetInstanceByDataReader(SqlDataReader reader)
        {
            TagInfo current = new TagInfo();
            // TagId,Name,DateCreated 
            current.TagId = reader.IsDBNull(0) ? 0 : reader.GetInt64(0);
            current.Name = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);
            current.DateCreated = reader.IsDBNull(2) ? DateTime.MinValue : reader.GetDateTime(2);


            return current;
        }

        #endregion
    }
}
